<template>
  <div class="login">
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="phone"
        label="用户名"
        placeholder="请输入手机号"
        :rules="[{ required: true, message: '请填写手机号' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="请输入密码"
        :rules="[{ required: true, message: '请输入密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { Toast } from "vant";
// import { reqaaa, reqbbb } from "../../api/loginreg";
import { setToken } from "../../utils/auth.js";
import axios from "axios";
export default {
  data() {
    return {
      username: "",
      password: "",
    };
  },
  computed: {},
  watch: {},

  methods: {
    async onSubmit(values) {
      console.log(111, "submit", values);
      // const result = await reqLoginAPI(values);
      // console.log(result);
      axios
        .get(
          `http://localhost:3000/login/cellphone?phone=${values.phone}&password=${values.password}`,
          {
            withCredentials: true,
          }
        )
        .then((result) => {
          console.log(result);
          if (result.data.code === 200) {
            Toast.success("登陆成功");
            console.log(result);
            localStorage.setItem("token", result.data.token);
            setToken(result.data.token);
            // 存入vuex
            // this.$store.commit("saveToken", result.data.token);
            this.$router.push("/home");
          } else {
            Toast.fail("密码错误");
          }
        });
    },
  },
  async created() {},
  mounted() {},
  components: {},
};
</script>
<style scoped>
.hed {
  background: #c20c0c;
}
</style>
